package com.mmall.annotation;

import sun.security.util.Length;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;


public class TestClass {

    private int age;

    @Bingo(max = 10,min=0,errorMesage = "errorMessage")
    private String name;
    @Bingo(max = 20,min=10,errorMesage = "errorMessage")
    private String password;

    public TestClass(int age, String name, String password) {
        this.age = age;
        this.name = name;
        this.password = password;
    }

    public static void main(String[] args) {

        TestClass testClass = new TestClass(10,"20gggggggggggggggggggggggg","20ggggggggggggggggggggggggggggggggggggggggggggggggggggggg");
        try {
            TestClass.validate(testClass);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

    }

    public static String  validate(Object object) throws IllegalAccessException {
        Field[] fields =  object.getClass().getDeclaredFields();
        for(Field field:fields){
            if(field.isAnnotationPresent(Bingo.class)){
                Bingo Bingo = field.getAnnotation(Bingo.class);
                field.setAccessible(true);
                int value = ((String)field.get(object)).length();
                if(value < Bingo.min()||value>Bingo.max()){
                    System.out.println(Bingo.errorMesage());
                    return Bingo.errorMesage();
                }
            }
        }
        return null;
    }
}
